package oj;

public class Num82 {
    //删除链表重复节点
    public ListNode deleteDuplication(ListNode pHead) {
        if(pHead == null){
            return null;
        }
        ListNode prev = null;
        ListNode node = pHead;
        ListNode nextNode = node.next;

        while (nextNode != null){
            if(node.val != nextNode.val){
                prev = node;
                node = nextNode;
                nextNode = nextNode.next;
            }else {
                while (nextNode != null && nextNode.val == node.val) {
                    nextNode = nextNode.next;
                }
                if (prev == null) {
                    pHead = nextNode;
                } else {
                    prev.next = nextNode;
                }
                while (node != nextNode) {
                    ListNode p = node;
                    node = node.next;
                }
                if (nextNode != null) {
                    nextNode = nextNode.next;
                }
            }
        }
        return pHead;
    }

    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummyHead = new ListNode(101);
        dummyHead.next = head;
        ListNode prev = dummyHead;
        ListNode node = dummyHead.next;

        while(node != null){
            ListNode nextNode = node.next;
            if(nextNode == null){
                return dummyHead.next;
            }
            if(node.val != nextNode.val){
                prev = prev.next;
                node = node.next;
            }else{
                while(nextNode != null && node.val == nextNode.val){
                    nextNode = nextNode.next;
                }
                prev.next = nextNode;
                node = nextNode;
            }
        }
        return dummyHead.next;
    }

}
